﻿
Imports std
Imports xmlctrl
Imports xhtmlctrl
Imports webctrl
Imports wfileis.data

Public Class submit
    Inherits controlProcessor

    Public Const _pathidentity As String = "/submit"

    Public Overrides Function pathidentity() As String
        Return _pathidentity
    End Function

    Private Shared Sub wrongInput(ByVal context As httpContext, ByVal page As xhtmlpage)
        assert(Not context Is Nothing, "context is nothing.")
        assert(Not page Is Nothing, "page is nothing.")

        page.title().text = context.string(stringId.SUBMIT_WRONGINPUT)
        page.body().text = context.string(stringId.SUBMIT_WRONTINPUT_DETAIL)
    End Sub

    Private Shared Sub failedSubmit(ByVal context As httpContext, ByVal page As xhtmlpage)
        assert(Not context Is Nothing, "context is nothing.")
        assert(Not page Is Nothing, "page is nothing.")

        page.title().text = context.string(stringId.SUBMIT_FAIL_TITLE)
        page.body().text = context.string(stringId.SUBMIT_FAIL_DETAIL)
    End Sub

    Protected Overrides Function processImpl(ByVal context As httpContext, ByRef control As xmlcontrol) As Boolean
        assert(Not context Is Nothing, "context is nothing.")

        Dim page As xhtmlpage = Nothing
        page = context.createControl(New xhtmlpage())

        Dim key As String = Nothing
        key = context.url().query(query.key)

        If Not isemptystring(key) AndAlso antiInputSpam.checkIpTable(query.checksum) Then
            Dim info As info = Nothing
            info = info.generate(context)

            Dim enableRecent As Boolean
            enableRecent = context.isinternalRequest() OrElse recentWhitelist.inlist(context.requestIp())

            Dim infokey As String = Nothing
            infokey = Convert.ToString(Now.Ticks())
            If Not info.uniqueWrite(infokey) Then
                raiseError("failed to write info to key " + infokey + ", ignore request.", _
                           errorHandle.errorType.exclamation)
                failedSubmit(context, page)
            Else
                Dim rec As record = Nothing
                rec = New record()
                rec.push_back(infokey)

                If Not rec.write(key) Then
                    failedSubmit(context, page)
                Else
                    If enableRecent Then
                        wfileis.recentTopic.insert(key, info.id, info.time)
                    End If

                    page.title().text = context.string(stringId.SUBMIT_TITLE)
                    Dim cats As vector(Of String) = Nothing
                    separateCategories(context, cats)
                    If Not isemptystring(info.id) Then
                        If cats Is Nothing Then
                            cats = New vector(Of String)()
                        End If
                        cats.push_back(info.id)
                    End If

                    Dim label As label = Nothing
                    If Not cats Is Nothing AndAlso cats.size() > 0 Then
                        Dim cat As category = Nothing
                        cat = New category()
                        cat.push_back(key)
                        Dim i As Int64
                        For i = 0 To cats.size() - 1
                            If Not cat.append(cats(i)) Then
                                raiseError("cannot append key to category, key " + key + ", category " + cats(i), _
                                           errorHandle.errorType.warning)
                                label = context.createControl(New label())
                                label.text = context.string(stringId.SUBMIT_FAIL_CATEGORY) + " " + cats(i)
                                page.body().addchild(label)
                                page.body().addchild(context.createControl(New br()))
                            Else
                                If Not strsame(cats(i), info.id, True) AndAlso enableRecent Then
                                    wfileis.recentCategory.insert(cats(i), info.id, info.time)
                                End If
                            End If
                        Next
                    End If

                    label = context.createControl(New label())
                    label.text = context.string(stringId.SUBMIT_SUCCESS)
                    page.body().addchild(label)
                    page.body().addchild(context.createControl(New br()))
                    page.body().style = "background-color: transparent;"

                    If context.url().queryExist(query.toviewpage) Then
                        Dim a As a = Nothing
                        a = context.createControl(New a())
                        a.text = context.string(stringId.TO_VIEW_NOW)
                        a.href = view.generateViewlink(context, key)
                        page.body().addchild(a)

                        Dim delayjump As delayJump = Nothing
                        delayjump = context.createControl(New delayJump())
                        delayjump.destination = a.href
                        delayjump.delay = 0
                        page.head().addchild(delayjump)
                    Else
                        Const submit_closeVar As String = "sbtcC"
                        page.inlineScript().addscript("submit_closeVar", _
                                                      webctrl.var.invoke(submit_closeVar) + ";")
                        Const submit_close_functionName As String = "sbtcls"
                        page.inlineScript().addscript("submit_close", _
                                                      "function " + submit_close_functionName + "(){" + _
                                                          "window.close();" + _
                                                          ifelse.invoke( _
                                                              existfunction.invoke(page, _
                                                                                   "'" + "parent." + _
                                                                                   writer.resultIFrameCallback _
                                                                                   + "'"), _
                                                              "parent." + _
                                                              writer.resultIFrameCallback + "(" + _
                                                                  webctrl.[true].name(page.inlineScript()) + _
                                                              ")") + _
                                                          clearTimeout.invoke(page.inlineScript(), _
                                                                              submit_closeVar) + _
                                                      ";}")
#If RETIRED Then
                     + _
                                                      clearTimeout.invoke(page.inlineScript(), _
                                                                          "parent." + writer.timeoutVar)
#End If
                        Dim a As a = Nothing
                        a = context.createControl(New emptyA())
                        a.text = context.string(stringId.CLOSE_10_SECS)
                        a.onclick = submit_close_functionName + "()"
                        page.body().addchild(a)

                        page.body().onload = submit_closeVar + "=" + _
                                             setTimeout.invoke(page, "'" + submit_close_functionName + "()'", _
                                                               secondsToMilliseconds(writer.timeout))
                    End If
                End If
            End If

            If Not isemptystring(info.id) Then
                context.setforevercookie(Nothing, Nothing, query.id, info.id)
            End If
        Else
            wrongInput(context, page)
        End If
        control = page
        Return True
    End Function
End Class
